package com.baidu.manager.modules.sys.controller;

import com.baidu.manager.common.Constant;
import com.baidu.manager.common.annotation.SysLog;
import com.baidu.manager.common.exception.JqlException;
import com.baidu.manager.common.utils.Result;
import com.baidu.manager.modules.sys.entity.SysMenu;
import com.baidu.manager.modules.sys.service.SysMenuService;
import com.baidu.manager.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Author: jql
 * @description: TODO(系统菜单)
 * @Date: Created by 14:56 2018/6/8
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 导航菜单
     *
     * @return
     */
    @RequestMapping("/nav")
    @RequiresPermissions("sys:menu:list")
    public Result nav() {
        List<SysMenu> menuList = sysMenuService.getUserMenuList(getUserId());
        Set<String> permissions = sysUserService.getUserPermissions(getUserId());
        return Result.ok().put("menuList", menuList).put("permissions", permissions);
    }

    /**
     * 所有菜单
     *
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<SysMenu> list() {
        List<SysMenu> menuList = sysMenuService.queryList(new HashMap<String, Object>());
        return menuList;
    }

    /**
     * 选择菜单（添加、修改菜单）
     *
     * @return
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:menu:select")
    public Result select() {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();

        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);
        return Result.ok().put("menuList", menuList);
    }

    /**
     * 菜单信息
     *
     * @param menuId
     * @return
     */
    @RequestMapping("info/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public Result info(@PathVariable("menuId") Long menuId) {
        SysMenu menu = sysMenuService.queryObject(menuId);
        return Result.ok().put("menu", menu);
    }

    @SysLog("保存菜单")
    @RequestMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public Result save(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);

        sysMenuService.save(menu);

        return Result.ok();
    }

    /**
     * 验证参数是否正确
     *
     * @param sysMenu
     */
    private void verifyForm(SysMenu sysMenu) {
        if (StringUtils.isBlank(sysMenu.getName())) {
            throw new JqlException("菜单名名称不能为空");
        }

        if (sysMenu.getParentId() == null) {
            throw new JqlException("上级菜单不能为空");
        }

        //菜单
        if (sysMenu.getType() == Constant.MenuType.MENU.getValue()) {
            if (StringUtils.isBlank(sysMenu.getUrl())) {
                throw new JqlException("菜单URL不能为空");
            }
        }

        //上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (sysMenu.getParentId() != 0) {
            SysMenu parentMenu = sysMenuService.queryObject(sysMenu.getParentId());
            parentType = parentMenu.getType();
        }

        //目录、菜单
        if (sysMenu.getType() == Constant.MenuType.CATALOG.getValue() || sysMenu.getType() == Constant.MenuType.MENU.getValue()) {
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new JqlException("上级菜单只能为目录类型");
            }
            return;
        }

        //按钮
        if (sysMenu.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new JqlException("上级菜单只能为菜单类型");
            }
            return;
        }
    }
}
